package com.sense.kukoo.biz_media.adapter.provider

import android.text.Html
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.sense.kukoo.biz_media.R
import com.sense.kukoo.biz_media.adapter.MediaListDetailAdapter
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.media.watcher.UserWatcher
import com.sense.kukoo.common.repo.bean.BookDetailsBean
import com.sense.kukoo.common.repo.bean.BookItemBean
import com.sense.kukoo.common.repo.bean.MediaListDetailItemBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_logger.LoggerManager
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class ShortAudioItemProvider(
    val listener: MediaListDetailAdapter.MediaListListener,
): MultiItemProvider<MediaListDetailItemBean>() {

    private val TAG: String = javaClass.simpleName

    private var mCurrentMusic: LocalMediaListItemBean? = null
    private var mCurrentState: Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING

    override fun getItemType(): Int = MediaListDetailItemBean.TYPE_SHORTAUDIO_LIST_ITEM

    override fun getItemLayoutId(): Int = R.layout.biz_media_item_short_audio_item

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: MediaListDetailItemBean) {
        val bookItems: BookItemBean = item.bookItems!!
        val bean: BookDetailsBean = item.bean!!
        //
        viewHolder.getView(R.id.root).setOnClickListener {
            listener.onBtnMediaItemClick(item)
        }
        //
        val iv_music_icon = viewHolder.getView(R.id.iv_music_icon) as ImageView
        val url = bookItems.coverUrl ?: bean.coverUrl
        Monet.get(iv_music_icon.context)
            .load<GlideConfig>(url)
            .placeholder(com.sense.kukoo.common.R.mipmap.common_icon_default_editor_picks)
            .into(iv_music_icon)
        //
        val tv_title_short_audio = viewHolder.getView(R.id.tv_title_short_audio) as TextView
        tv_title_short_audio.text = bookItems.name
        //
        val iv_music_playing = viewHolder.getView(R.id.iv_music_playing) as ImageView
        val tv_min_left = viewHolder.getView(R.id.tv_min_left) as TextView
        val tv_min_total = viewHolder.getView(R.id.tv_min_total) as TextView
        if(mCurrentMusic != null && mCurrentMusic?.idOfBooks() == bookItems.bookId && mCurrentMusic?.idOfChapter() == bookItems.id){
            //当前音乐
            when (mCurrentState) {
                UserWatcher.MusicStateWatcher.STATE_PREPARE,
                UserWatcher.MusicStateWatcher.STATE_ERROR_RETRYING,
                UserWatcher.MusicStateWatcher.STATE_SEEKING,
                UserWatcher.MusicStateWatcher.STATE_PLAYING -> {
                    //播放中
                    iv_music_playing.visibility = View.VISIBLE
                    Monet.get(iv_music_playing.context)
                        .load<GlideConfig>(com.sense.kukoo.common.R.mipmap.common_icon_wave_red)
                        .into(iv_music_playing)
                }
                else -> {
                    //暂停，其它状态
                    iv_music_playing.visibility = View.GONE
                }
            }
            //
            tv_min_left.visibility = View.VISIBLE
            tv_min_total.visibility = View.GONE
            //
            var len:Int = (bookItems.duration ?: 0) - (bookItems.listenTotal ?: 0)
            len = len/60
            len = if(len <=0 ) 1 else len
            tv_min_left.setText(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.library_min_left, len))
        }else{
            //非当前
            iv_music_playing.visibility = View.GONE
            tv_min_left.visibility = View.GONE
            val time = calculateTime(bookItems.duration ?: 0)
            tv_min_total.visibility = View.VISIBLE
            tv_min_total.setText(time)
        }
        //
        val tv_listens = viewHolder.getView(R.id.tv_listens) as TextView
        tv_listens.text = "" + bookItems.listeners
        //
        val tv_like = viewHolder.getView(R.id.tv_like) as TextView
        tv_like.text = "" + bookItems.likes
    }

    fun setCurrentMusic(music: LocalMediaListItemBean){
        mCurrentMusic = music
    }

    fun setCurrentPlayState(state: Int){
        mCurrentState = state
    }

    private fun calculateTime(time: Int): String {
        return if (time >= 60) {
            val minute = time / 60
            val second = time % 60
            (if (minute < 10) "0$minute" else "" + minute) + if (second < 10) ":0$second" else ":$second"
        } else {
            if (time < 10) "00:0$time" else "00:$time"
        }
    }

}